// pages/business/business.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
    navs:[],
    lists:[],
    page:1,
    lastPage:0,
    cid:0,
    curindex:0,
    longitude: '',
    latitude: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkSetting();
 
    // this.getShop();
  },
  click_back(){
    wx.navigateBack({
      delta: 1,
    })
  },

  bottomBtn(e){
    let index=e.currentTarget.dataset.index;
    let listIndex=e.currentTarget.dataset.list_index;
    let info = this.data.lists[listIndex]
    if(index == 0){
      wx.openLocation({
        latitude: parseFloat(info.latitude),
        longitude: parseFloat(info.longitude),
        address:info.address,
        name:info.name,
        fail:function(err){
          console.log(err)
        }
      })
    }else if(index == 1){
      wx.makePhoneCall({
        phoneNumber: info.phone,
      })
    }else if(index == 2){
      wx.navigateTo({
        url: '/pages/select_service/select_service?shop_id='+this.data.lists[listIndex].id,
      })
    }
  },
  shopCategory(){
    api.get('/common/businessCategory').then(res=>{
      this.setData({
        navs:res.items,
        cid:res.items ? res.items[0].id : 0
      })
      this.getShop();
    })
  },
  getShop(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/common/getShops',{
      longitude:this.data.longitude,
      latitude:this.data.latitude
    }).then(res=>{
      this.setData({
        moneys:res.moneys,
        orderCount:res.order_count,
        lists:res.shops,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
    })
  },
  click_nav(e){
    const index=e.currentTarget.dataset.index;
  
    let that = this;
    console.log(that.data.navs)
    this.setData({
      curindex:index,
      cid:that.data.navs[index].id,
      lists:[],
      page:1,
      lastPage:0
    })
    
  },
  toDetial(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/store_detail/store_detail?id='+id,
    })
  },
  //获取定位
  checkSetting() {
    let that = this;
    wx.getSetting({
      success(res) {
        console.log("getSetting", res);
        if (!res.authSetting['scope.userLocation']) {
          // 接口调用询问  
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              that.getLocation();
            },
            fail() {
              // 用户拒绝了授权  
              that.getShop();
              wx.showModal({
                title: '提示',
                content: '拒绝后无法为你推荐实时定位信息，请授权定位信息',
                success(res) {
                  if (res.confirm) {
                    // 打开设置页面  
                    wx.openSetting({
                      success: function (data) {
                        console.log("openSetting: success");
                      },
                      fail: function (data) {
                        console.log("openSetting: fail", data);
                      }
                    });
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
        } else {
          console.log("1-已经授权权限");
          that.getLocation()
        }
      },
      fail(res) {
        console.log("getSetting: success");
        console.log(res);
      }
    })

  },
  getLocation() {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        wx.setStorageSync("Location", res)
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
        that.getShop();
      },
      fail(res) {
        that.getShop();
        console.log('位置 失败', res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})